package com.sinosoft.perfect.system._roleBusiness.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sinosoft.perfect.system._.po.TRoleBusiness;
import com.sinosoft.perfect.system._.service.ISortNumberService;
import com.sinosoft.perfect.system._.util.Producer;
import com.sinosoft.perfect.system._.vo.SortNumber;
import com.sinosoft.perfect.system._roleBusiness.dao.IRoleBusinessDAO;
import com.sinosoft.perfect.system._roleBusiness.vo.AllUsersInOneRoleBusinessVO;
import com.sinosoft.perfect.system._roleBusiness.vo.RoleBusinessVO;

public class RoleBusinessServiceImpl implements IRoleBusinessService {

	private IRoleBusinessDAO roleBusinessDAO;

	private ISortNumberService sortNumberService;

	public void setSortNumberService(ISortNumberService sortNumberService) {
		this.sortNumberService = sortNumberService;
	}

	public void setRoleBusinessDAO(IRoleBusinessDAO roleBusinessDAO) {
		this.roleBusinessDAO = roleBusinessDAO;
	}

	public void deleteTRoleBusinessByPrimaryKey(TRoleBusiness troleBusiness) throws SQLException {
		roleBusinessDAO.deleteTRoleBusinessByPrimaryKey(troleBusiness);
	}

	public String insertByTRoleBusiness(TRoleBusiness troleBusiness) throws SQLException {
		int sortNum = this.selectTRoleBusinessMAXSortNum() + 1;
		String id = Producer.generateIdOfTen();
		troleBusiness.setRoleBusinessId(id);
		troleBusiness.setStatus("1");
		troleBusiness.setBusinessSortNumber(new Long(sortNum));
		roleBusinessDAO.insertByTRoleBusiness(troleBusiness);
		return id;
	}

	public Integer selectTRoleBusinessMAXSortNum() throws SQLException {
		Integer sortNum = 0;
		Object numObj = roleBusinessDAO.selectTRoleBusinessMAXSortNum();
		if (numObj != null)
			sortNum = Integer.parseInt(numObj.toString());
		return sortNum;
	}

	public List<TRoleBusiness> selectByTRoleBusiness(TRoleBusiness troleBusiness, int startIndex, int endIndex) throws SQLException {
		List<TRoleBusiness> TRoleBusinesss = roleBusinessDAO.selectByTRoleBusiness(troleBusiness, startIndex, endIndex);
		return TRoleBusinesss;
	}

	public List<TRoleBusiness> selectByTRoleBusiness(TRoleBusiness troleBusiness) throws SQLException {
		List<TRoleBusiness> TRoleBusinesss = roleBusinessDAO.selectByTRoleBusiness(troleBusiness);
		return TRoleBusinesss;
	}

	public List<String> selectTRoleBusinessFromUnit(String unitId) throws SQLException {
		List<RoleBusinessVO> roleBusinessVOs = roleBusinessDAO.selectTRoleBusinessFromUnit(unitId);
		List<String> roleBusinessIds = null;
		if (roleBusinessVOs != null && roleBusinessVOs.size() != 0) {
			roleBusinessIds = new ArrayList<String>();
			for (int i = 0; i < roleBusinessVOs.size(); i++) {
				roleBusinessIds.add(roleBusinessVOs.get(i).getRoleBusiId());
			}
		}
		return roleBusinessIds;
	}

	public List<String> selectTRoleBusinessFromUser(String userId) throws SQLException {
		List<RoleBusinessVO> roleBusinessVOs = roleBusinessDAO.selectTRoleBusinessFromUser(userId);
		List<String> roleBusinessIds = null;
		if (roleBusinessVOs != null && roleBusinessVOs.size() != 0) {
			roleBusinessIds = new ArrayList<String>();
			for (int i = 0; i < roleBusinessVOs.size(); i++) {
				roleBusinessIds.add(roleBusinessVOs.get(i).getRoleBusiId());
			}
		}
		return roleBusinessIds;
	}

	public void updateTRoleBusinessByPrimaryKey(TRoleBusiness troleBusiness) throws SQLException {
		roleBusinessDAO.updateTRoleBusinessNotNullByPrimaryKey(troleBusiness);
	}

	public Long selectTRoleBusinessSortNum(String roleBusinessId) throws SQLException {
		Long sortNum = null;
		Object obj = roleBusinessDAO.selectTRoleBusinessSortNum(roleBusinessId);
		if (obj != null)
			sortNum = Long.parseLong(obj.toString());
		return sortNum;
	}

	public boolean sortRoleBusiness(long newSortNumber, String roleBusinessId) throws SQLException {
		SortNumber sortNumber = new SortNumber(newSortNumber, roleBusinessId, "T_ROLE_BUSINESS", "ROLE_BUSINESS_ID", "", "BUSINESS_SORT_NUMBER");
		sortNumberService.sortNumberNoSuper(sortNumber);
		return false;
	}

	@Override
	public String selectExitsUsers(TRoleBusiness tRoleBusiness) throws SQLException {
		List<AllUsersInOneRoleBusinessVO> allUsersInOneRoleBusinessVOs =  roleBusinessDAO.selectExitsUsers(tRoleBusiness);
		StringBuffer userIds = new StringBuffer("");
		if (allUsersInOneRoleBusinessVOs != null && allUsersInOneRoleBusinessVOs.size() > 0 ) {
			for (int i = 0; i < allUsersInOneRoleBusinessVOs.size(); i++) {
				AllUsersInOneRoleBusinessVO allUsersInOneRoleBusinessVO = allUsersInOneRoleBusinessVOs.get(i);
				if(allUsersInOneRoleBusinessVO != null && null != allUsersInOneRoleBusinessVO.getUserMainId() && !"".equals(allUsersInOneRoleBusinessVO.getUserMainId())){
					userIds.append(allUsersInOneRoleBusinessVOs.get(i).getUserMainId());
					if(i < allUsersInOneRoleBusinessVOs.size()-1){
						userIds.append(",");
					}
				}
			}
		}
		return userIds.toString();
	}

}
